import { JsonAsset } from "cc";
import { Level } from "../data/GameObjects";
import { ResMgr } from "../manager/ResMgr";
import Cfgs from "../configs/Cfgs";

/**
 * 游戏数据中心
 */
class GameData {
    private levelList: Level[] = null;
    private levelMap: { [key: number]: Level } = {};
    // private _level: Level = null;

    public get firstLevelId() {
        // return this.getLevel(1,1).getLevelId();
        // return Cfgs.Level.getFirstId();
        return 1;
    }
    public hasLevel(levelId: number, difficult:1|2|3|4 = 1) {
        console.log('### GameData hasLevel', levelId, difficult);
        // return !!this.levelMap[levelId];
        // return Cfgs.Level.getLevelId(levelId, difficult);
        return true;
    }
    // public getNextLevel(curLevelId: number, difficult:1|2|3|4 = 1) {
    //     console.log('### GameData getNextLevel', curLevelId, difficult);
    //     // const level = this.levelList.find(e=>{
    //     //     return e.getLevelId()>curLevelId && e.getDifficult()==difficult;
    //     // });
    //     // return level;
    //     // const levelId = Cfgs.Level.getLevelId(curLevelId + 1, difficult);
    //     // if (!levelId) {
    //     //     return curLevelId;
    //     // }
    //     // const level = this.levelMap[levelId];
    //     // level.levelId = curLevelId + 1;
    //     // return level;
    //     return this.getLevel(curLevelId + 1, difficult);
    // }
    
    public loadAllLevelData(onProgress: (finished: number, total: number) => void) {
        return new Promise<void>((resolve, reject) => {
            ResMgr.instance.loadDir('config','level',onProgress,(err, assets: JsonAsset[])=>{
                if (err) {
                    reject();
                    return;
                }
                const list = assets.map(e=>Object.assign(new Level(), e.json));
                this.levelList = [...list];
                this.levelList.sort((a, b) => a.id - b.id);
                // 将levelList放进levelMap，id作为key
                this.levelMap = {};
                for (let i = 0; i < this.levelList.length; i++) {
                    this.levelMap[this.levelList[i].id] = this.levelList[i];
                }
                resolve();
                console.log('### GameData loadAllLevelData done');
            });
        });
    }

    // public loadLevelData(levelId: number) {
    //     return new Promise<void>((resolve, reject)=>{
    //         const item = Cfgs.Level.getItemByLevel(levelId);
    //         if (!item) {
    //             return resolve(null);
    //         }
    //         let names: string[] = [];
    //         for (let i = 1; i <= 4; i++) {
    //             const name = item['difficult'+i];
    //             if (names.indexOf(name) == -1) {
    //                 names.push(name);
    //             }
    //         }
    //         const paths = names.map(e=>'level/level'+e);
    //         console.log('### GameData loadLevelData', paths);
    //         ResMgr.instance.load('config',paths, JsonAsset, (err, assets: JsonAsset[]) => {
    //             if (err) {
    //                 console.log('### GameData loadLevelData error', err);
    //                 reject();
    //                 return;
    //             }
    //             const list = assets.map(e=>Object.assign(new Level(), e.json));
    //             console.log('### GameData loadLevelData list', list);
    //             this.levelMap = this.levelMap || {};
    //             for (let i = 0; i < list.length; i++) {
    //                 this.levelMap[list[i].id] = list[i];
    //             }
    //             resolve();
    //         })
    //     });
    // }

    // public getLevel(levelId: number, difficult:1|2|3|4 = 1): Level {
    //     let id = Cfgs.Level.getLevelId(levelId, difficult);
    //     if (!id || !this.levelMap[id]) {
    //         return null;
    //     }
    //     if (!this.levelMap[id]) {
    //         return null;
    //     }
    //     const level = Object.assign(new Level(), this.levelMap[id]);
    //     level.tableCards = Object.assign([], level.tableCards);
    //     for (let i = 0; i < level.tableCards.length; i++) {
    //         level.tableCards[i] = Object.assign(new Card(), level.tableCards[i]);
    //     }
    //     level.levelId = levelId;
    //     return level;
    // }

    // public getLevelAsync(levelId: number, difficult:1|2|3|4 = 1): Promise<Level> {
    //     return new Promise<Level>((resolve, reject)=>{
    //         let level = this.getLevel(levelId, difficult);
    //         if (level) {
    //             resolve(level);
    //         } else {
    //             this.loadLevelData(levelId).then(()=>{
    //                 level = this.getLevel(levelId, difficult);
    //                 resolve(level);
    //             }).catch(()=>{
    //                 // reject();
    //                 resolve(null);
    //             });
    //         }
    //         if (!this.getLevel(levelId + 1, 1)) {
    //             this.loadLevelData(levelId + 1);
    //         }
    //     });
    // }
}

export default new GameData();